/**
 * 通用三键弹出框
 * created by Five on 2018-12-17 10:57:19
 */

const { ccclass, property } = cc._decorator;

@ccclass
export default class Popup extends cc.Component {
    private button1: cc.Button = null;
    private button2: cc.Button = null;
    private button3: cc.Button = null;

    private btn_label1: cc.Label = null;
    private btn_label2: cc.Label = null;
    private btn_label3: cc.Label = null;

    @property(cc.Label)
    context: cc.Label = null;

    start() {
        this.button1 = this.getButton("button1");
        this.button2 = this.getButton("button2");
        this.button3 = this.getButton("button3");
        this.btn_label1 = this.getLabel(this.button1.node, "Label");
        this.btn_label2 = this.getLabel(this.button2.node, "Label");
        this.btn_label3 = this.getLabel(this.button3.node, "Label");

        this.Close();
    }

    Close() {
        this.node.active = false;
    }

    init() {
        this.Hide(this.button1.node);
        this.Hide(this.button2.node);
        this.Hide(this.button3.node);
        this.button1.clickEvents.length = 0;
        this.button2.clickEvents.length = 0;
        this.button3.clickEvents.length = 0;
        this.node.active = true;
    }

    /**
     * 单个按钮
     * @param context 文本内容
     * @param btncontext 按钮文本
     * @param callback 按钮回调
     */
    public OneButton(context: string, btncontext: string, callback: Function) {
        this.init();
        this.Show(this.button2.node);
        this.context.string = context;
        this.btn_label2.string = btncontext;
        this.button2.node.on("click", callback);
    }

    public TwoButton(context: string, btncontext1: string, callback1: Function,
        btncontext2: string, callback2: Function) {
        this.init();
        this.Show(this.button1.node);
        this.Show(this.button3.node);
        this.context.string = context;
        this.btn_label1.string = btncontext1;
        this.button1.node.on("click", callback1);
        this.btn_label3.string = btncontext2;
        this.button3.node.on("click", callback2);
    }

    public ThreeButton(context: string, btncontext1: string, callback1: Function,
        btncontext2: string, callback2: Function,
        btncontext3: string, callback3: Function) {
        this.init();
        this.Show(this.button1.node);
        this.Show(this.button2.node);
        this.Show(this.button3.node);
        this.context.string = context;
        this.btn_label1.string = btncontext1;
        this.button1.node.on("click", callback1);
        this.btn_label2.string = btncontext2;
        this.button2.node.on("click", callback2);
        this.btn_label3.string = btncontext3;
        this.button3.node.on("click", callback3);
        this.context.string = context;
    }

    public Hide(node: cc.Node) {
        node.active = false;
    }

    public Show(node: cc.Node) {
        node.active = true;
    }

    private getLabel(parent: cc.Node, name: string): cc.Label {
        return this.getChild(parent, name).getComponent(cc.Label);
    }

    private getButton(name: string): cc.Button {
        return this.getChild(this.node, name).getComponent(cc.Button);
    }

    private getChild(parent: cc.Node, name: string): cc.Node {
        return parent.getChildByName(name);
    }
}